package com.xjrh.traintravel.collector.common;

import java.io.File;
import java.io.Serializable;

/**
 * 文件读取Bean
 *  <p>
 *      用于分次读取文件内容，内部维护读取进度
 *  </p>
 * @author snps
 * @create 2022-01-25 16:15
 * @describe
 **/
public class ReadFileBean implements Serializable {


    /**
     * 文件
     */
    private File file;

    /**
     * 文件名
     */
    private String fileName;

    /**
     * 文件总大小（字节数）
     */
    private long fileByteLength;

    /**
     * 每次读取字节数
     */
    private int readByteLength;

    /**
     * 文件需要分多少次可以读取完（等于：文件总字节数 / 每次读取字节数
     */
    private int needReadNumber;

    /**
     * 已读取次数
     */
    private int alreadyReadNumber;

    /**
     * 已读取字节数
     */
    private long alreadyReadByteLength;

    /**
     * 剩余未读取字节数
     */
    private long noReadByteLength;

    /**
     * 内容开始位置
     */
    private long startPos;

    /**
     * 内容结尾位置
     */
    private long endPos;

    /**
     * 本次读取的字节数组
     */
    private byte[] bytes;


    /*********************************************
     * Constructor
     */
    public ReadFileBean() {
    }

    public ReadFileBean(String fileName, long fileByteLength, int readByteLength) {
        this.fileName = fileName;
        this.fileByteLength = fileByteLength;
        this.readByteLength = readByteLength;

        // 设置：需要读取文件次数
        if (fileByteLength == 0 || readByteLength == 0) {
            this.needReadNumber = 0;
        } else {
            long remainder = fileByteLength % readByteLength;
            if (remainder == 0) {
                this.needReadNumber = (int) fileByteLength / readByteLength;
            } else {
                this.needReadNumber = ((int) (fileByteLength / readByteLength)) + 1;
            }
        }

        // 设置：内容开始位置(需要跳过的字节数)
        this.startPos = 0;
        // 设置：已读取次数
        this.alreadyReadNumber = 0;
        // 设置：已读取字节数
        this.alreadyReadByteLength = 0;
        // 设置：剩余未读取字节数
        this.noReadByteLength = fileByteLength;
    }


    /*********************************************
     * Getter and Setter
     */
    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public long getFileByteLength() {
        return fileByteLength;
    }

    public void setFileByteLength(long fileByteLength) {
        this.fileByteLength = fileByteLength;
    }

    public int getReadByteLength() {
        return readByteLength;
    }

    public void setReadByteLength(int readByteLength) {
        this.readByteLength = readByteLength;
    }

    public int getNeedReadNumber() {
        return needReadNumber;
    }

    public void setNeedReadNumber(int needReadNumber) {
        this.needReadNumber = needReadNumber;
    }

    public int getAlreadyReadNumber() {
        return alreadyReadNumber;
    }

    public void setAlreadyReadNumber(int alreadyReadNumber) {
        this.alreadyReadNumber = alreadyReadNumber;
    }

    public long getAlreadyReadByteLength() {
        return alreadyReadByteLength;
    }

    public void setAlreadyReadByteLength(long alreadyReadByteLength) {
        this.alreadyReadByteLength = alreadyReadByteLength;
    }

    public long getNoReadByteLength() {
        return noReadByteLength;
    }

    public void setNoReadByteLength(long noReadByteLength) {
        this.noReadByteLength = noReadByteLength;
    }

    public long getStartPos() {
        return startPos;
    }

    public void setStartPos(long startPos) {
        this.startPos = startPos;
    }

    public long getEndPos() {
        return endPos;
    }

    public void setEndPos(long endPos) {
        this.endPos = endPos;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

}